/**
 * 
 */
package form.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import form.bean.PromotionForm;

/**
 * Classe de validation du bean PromotionForm
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class AddPromoFormValidator implements Validator {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean supports(Class clazz) {
		return PromotionForm.class.isAssignableFrom(clazz);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#validate(java.lang.Object,
	 * org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object obj, Errors e) {
		PromotionForm promotionForm = (PromotionForm) obj;

		/** Validation de l'anne de la promotion */
		ValidationUtils.rejectIfEmptyOrWhitespace(e, "promoYear", "key",
				"année promotion vide");

		if (!e.hasFieldErrors("promoYear")) {
			if (!FieldValidatorUtil.isValidPromoYear(promotionForm
					.getPromoYear())) {
				e.rejectValue("promoYear", "key",
						"L'année doit être de la forme 2000/2001");
			}
		}

		/** Validation de l'intitule de la promo */
		ValidationUtils.rejectIfEmpty(e, "title", "key",
				"intitulé promotion vide");

		if (!e.hasFieldErrors("title")) {
			if (!FieldValidatorUtil.isValidText(promotionForm.getTitle())) {
				e
						.rejectValue("title", "key",
								"L'intitulé de la promotion contient des caractères interdits");
			}
		}

		/** Validation de la description de la promo */
		if (!promotionForm.getDescription().equals("")) {

			if (!FieldValidatorUtil.isValidText(promotionForm.getDescription())) {
				e
						.rejectValue("description", "key",
								"La description de la promotion contient des caractères interdits");
			}
		}

	}

}
